//
//  firstConfigViewController.m
//  MMWMiotLinkDemo
//
//  Created by   HGG  on 16/1/19.
//  Copyright © 2016年 HGG. All rights reserved.
//

#import "firstConfigViewController.h"

@interface firstConfigViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *deviceIPTF;
@property (weak, nonatomic) IBOutlet UITextField *devicePortTF;
@property (weak, nonatomic) IBOutlet UITextField *deviceMacTF;

@property (weak, nonatomic) IBOutlet UITextField *timeOutTF;
@property (weak, nonatomic) IBOutlet UILabel *configResultLB;

@end

@implementation firstConfigViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _deviceIPTF.text = _device.ip;
    _devicePortTF.text = [NSString stringWithFormat:@"%@",@(_device.port)];
    _deviceMacTF.text = _device.mac;
    
}

-(void)setDevice:(MMWMiotLinkDeviceInfo *)device{
    if (!_device) {
        _device = [MMWMiotLinkDeviceInfo new];
    }
    _device.ip = device.ip;
    _device.port = device.port;
    _device.mac = device.mac;

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    
}
- (IBAction)textFieldDidEndEditing:(id)sender {
    if ( sender == _timeOutTF) {
        CGRect bounds = self.view.bounds;
        bounds.origin.y = 0.f;
        self.view.bounds = bounds;
    }
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == _timeOutTF ) {
        CGRect bounds = self.view.bounds;
        bounds.origin.y = 100.f;
        self.view.bounds = bounds;
    }
    return YES;
}


- (IBAction)configPlatformAction:(id)sender {
    MMWMiotLinkDeviceInfo *device = [[MMWMiotLinkDeviceInfo alloc] init];
    device.ip = self.deviceIPTF.text;
    device.mac = self.deviceMacTF.text;
    device.port = [self.devicePortTF.text intValue];
    
    __weak typeof(self)weakSelf = self;
    
    [[MMWMiotLink shareInstence]mmw_MiotLinkFirstConfig:device userPlatformMode:0 userPlatformURL:nil userPlatformPort:nil serial:nil time:[self.timeOutTF.text intValue] callBack:^(int resultCode) {
        NSLog(@"step_one:配置成功");
        __strong typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.configResultLB.text = [NSString stringWithFormat:@"result = %d", resultCode];
        if (resultCode)
        {
            [strongSelf.navigationController popToRootViewControllerAnimated:YES];
        }
 }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
